package com.emapp.lccode.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.emapp.lccode.R
import com.emapp.lccode.bean.ProblemTypeKt
import com.emapp.lccode.databinding.ListitemTypePopBinding
import com.emapp.lccode.utils.vertical

class DialogTypeMapAdapter : BaseQuickAdapter<ProblemTypeKt, DialogTypeMapAdapter.VH>() {
    var listener: RefreshListener? = null

    class VH(
        parent: ViewGroup,
        val binding: ListitemTypePopBinding = ListitemTypePopBinding.inflate(
            LayoutInflater.from(parent.context), parent, false
        ),
    ) : RecyclerView.ViewHolder(binding.root)

    override fun onCreateViewHolder(context: Context, parent: ViewGroup, viewType: Int): VH {
        return VH(parent)
    }

    override fun onBindViewHolder(holder: VH, position: Int, item: ProblemTypeKt?) {
        if (item == null) return

        val adapter1: DialogTypeMapAdapter = DialogTypeMapAdapter()
        adapter1.listener = listener
        if (item.child!=null&&item.child.size > 0) {
            item.child.forEach {
                if (it.isCheck) {
                    item.isCheck = true
                }
            }
            adapter1.submitList(item.child)
            adapter1.notifyDataSetChanged()
            holder.binding.ivRight.visibility = View.VISIBLE
        } else {
            holder.binding.ivRight.visibility = View.GONE
        }
        holder.binding.tvName.text = item.itemLabel
        if (item.isCheck) {
            holder.binding.ivImage.setImageResource(R.drawable.outline_check_box_24)
        } else {
            holder.binding.ivImage.setImageResource(R.drawable.outline_check_box_outline_blank_24)
        }
        if (item.open) {
            holder.binding.rvList.visibility = View.VISIBLE
            holder.binding.ivRight.setImageResource(R.drawable.baseline_arrow_drop_down_24)
        } else {
            holder.binding.rvList.visibility = View.GONE
            holder.binding.ivRight.setImageResource(R.drawable.baseline_arrow_right_24)
        }
        holder.binding.lvName.setOnClickListener {
            if (item.child != null && item.child.size > 0) {
                item.open = !item.open
                notifyDataSetChanged()
            } else {
                item.isCheck = !item.isCheck
                if (item.child != null && item.child.size > 0) {
                    item.child.forEach {
                        it.isCheck = item.isCheck
                    }
                }
                listener?.onRefresh()
                notifyDataSetChanged()
            }
        }
        holder.binding.ivImage.setOnClickListener {
            item.isCheck = !item.isCheck
            if (item.child != null && item.child.size > 0) {
                item.child.forEach {
                    it.isCheck = item.isCheck
                }
            }
            listener?.onRefresh()
            notifyDataSetChanged()
        }
        holder.binding.rvList.run {
            vertical()
            adapter = adapter1
        }

        holder.binding.ivLine.visibility =
            if (position == itemCount - 1) View.GONE else
                View.VISIBLE
    }

    interface RefreshListener {
        fun onRefresh()
    }
}